/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/

#include <QDate>
#include <QFile>
#include <QMessageBox>
#include <QSet>

#include <billitem.h>

#include "reportoptionsdialog.h"
#include "reportwindow.h"
#include "reports.h"

Q_EXPORT_PLUGIN2(plugin_icalexport, Reports)

Reports::Reports()
{
    m_lib = 0;

    m_actionRun = new QAction(tr("Generate Report"), 0);
    m_options = new ReportOptions();
}

Reports::~Reports()
{
    delete m_actionRun;
    delete m_options;
}

void Reports::setLib(NiwBillManager *lib)
{
    m_lib = lib;
}

QAction *Reports::getAction()
{
    return m_actionRun;
}

QString Reports::getName()
{
    return tr("Reports");
}

QString Reports::getCredits()
{
    // Default credits for the application.
    // Included in niw-bill-manager app as a resource.
    QFile creditsFile(":/plugin-reports/about/credits.html");
    creditsFile.open(QIODevice::ReadOnly);
    QString credits = creditsFile.readAll();
    creditsFile.close();

    return credits;
}

QString Reports::getDescription()
{
    return tr("Creates detailed reports.");
}

QString Reports::getVersion()
{
    return "1.0";
}

QString Reports::getLicense()
{
    // Default license of the application.
    // Included in niw-bill-manager app as a resource.
    QFile licenseFile(":/plugin-reports/about/gpl-3.0-standalone.html");
    licenseFile.open(QIODevice::ReadOnly);
    QString license = licenseFile.readAll();
    licenseFile.close();

    return license;
}

bool Reports::canConfig()
{
    return false;
}

void Reports::execConfigDialog()
{
}

void Reports::on_actionRun_triggered()
{
    if (!m_lib) {
        QMessageBox::critical(0, tr("Error"), tr("Lib not loaded.\n"
            "Cannot query bills."));
        return;
    }

    QSet<QString> billDates;

    m_lib->startGetAllBillItems();
    while (m_lib->hasMoreBillItems()) {
        BillItem billItem;
        m_lib->getNextBillItem(billItem);

        QDate date(billItem.getDateDue().year(), billItem.getDateDue().month(),
            1);
        billDates.insert(date.toString(Qt::ISODate));
    }

    ReportOptionsDialog reportOptionsDialog;

    reportOptionsDialog.setDates(billDates);
    reportOptionsDialog.setOptions(*m_options);
    reportOptionsDialog.exec();

    if (reportOptionsDialog.result() == QDialog::Accepted) {
        *m_options = reportOptionsDialog.getOptions();
        QList<QDate> dates = reportOptionsDialog.getDates();

        if (checkOptions(*m_options) && checkDates(dates)) {
            ReportWindow *reportWindow = new ReportWindow();

            reportWindow->setDates(dates);
            reportWindow->setOptions(*m_options);
            reportWindow->setLib(m_lib);
            reportWindow->generateReport();
            reportWindow->show();
        }
    }
}

bool Reports::checkOptions(const ReportOptions &options)
{
    if (!options.optionPaid && !options.optionUnpaid) {
        QMessageBox::critical(0, tr("Error Generating Report"),
            "One or both options Paid / Unpaid must be selected. Otherwise,"
            "no bills will be included in the report.");
        return false;
    }
    if (!options.optionRecurring && !options.optionNonRecurring) {
        QMessageBox::critical(0, tr("Error Generating Report"),
            "One or both options Recurring / Non-Recurring must be selected."
            "Otherwise, no bills will be included in the report.");
        return false;
    }
    if (!options.optionAutoPay && !options.optionNonAutoPay) {
        QMessageBox::critical(0, tr("Error Generating Report"),
            "One or both options Auto Pay / Non-Auto Pay must be selected."
            "Otherwise, no bills will be included in the report.");
        return false;
    }

    return true;
}

bool Reports::checkDates(const QList<QDate> &dates)
{
    if (dates.isEmpty()) {
        QMessageBox::critical(0, tr("Error Generating Report"),
            "No dates have been selected.");
        return false;
    }

    return true;
}
